#include "Board.h"

Board::Board(): cells(0), boardWidth(0), boardHeight(0)
{
}


Board::~Board()
{
}

unsigned int Board::Size() const
{
	return boardWidth * boardHeight;
}

void Board::Clear()
{
	if (cells)
	{
		delete[] cells;

		cells = 0;
	}
}


void Board::Resize(unsigned int width, unsigned int height)
{
	Clear();

	boardWidth = width;
	boardHeight = height;

	cells = new Cell[Size()];
}

void Board::Move(GameObject* object, unsigned int cellX, unsigned int cellY)
{
	if (InsideBoard(cellX, cellY))
	{
		object->MoveToCell(GetCell(cellX, cellY), cellX, cellY);
	}
	else
	{
		object->MoveToCell(0, cellX, cellY);
	}
}

bool Board::InsideBoard(unsigned int cellX, unsigned int cellY)
{
	return cellX < boardWidth && cellY < boardHeight;
}

Cell* Board::GetCell(unsigned int cellX, unsigned int cellY)
{
	return &cells[ Index(cellX, cellY) ];
}

unsigned int Board::Index(unsigned int cellX, unsigned int cellY)
{
	if (!InsideBoard(cellX, cellY))
	{
		throw "Board: Cell position outside of board";
	}

	return cellX + cellY * boardWidth;
}

void Board::Print()
{
	for(unsigned int y=0; y<boardHeight; y++)
	{
		for(unsigned int x=0; x<boardWidth; x++)
		{
			printf("%s", GetCell(x, y)->ToString().c_str());
		}

		printf("\n");
	}
}
